package com.che.software.testato.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.che.software.testato.business.ActivityManager;
import com.che.software.testato.business.exception.ActivityDocumentationCreationManagerException;
import com.che.software.testato.domain.entity.Activity;
import com.che.software.testato.domain.entity.ActivityAction;
import com.che.software.testato.domain.entity.ActivityParameter;
import com.che.software.testato.web.controller.acontroller.AbstractController;

/**
 * Controller used to manage the activities documentation.
 * 
 * @author Clement HELIOU (clement.heliou@che-software.com).
 * @copyright Che Software.
 * @license GNU General Public License.
 * @see AbstractController, Serializable.
 * @since July, 2011.
 * 
 *        This file is part of Testato.
 * 
 *        Testato is free software: you can redistribute it and/or modify it
 *        under the terms of the GNU General Public License as published by the
 *        Free Software Foundation, either version 3 of the License, or (at your
 *        option) any later version.
 * 
 *        Testato is distributed in the hope that it will be useful, but WITHOUT
 *        ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *        FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 *        for more details.
 * 
 *        You should have received a copy of the GNU General Public License
 *        along with Testato. If not, see <http://www.gnu.org/licenses/>.
 * 
 *        Testato's logo is a creation of Arrioch
 *        (http://arrioch.deviantart.com/) and it's distributed under the terms
 *        of the Creative Commons License.
 */
@Component("activityDocumentationController")
@ManagedBean(name = "activityDocumentationController")
@Scope("session")
@SessionScoped
public class ActivityDocumentationController extends AbstractController implements Serializable {

	/**
	 * Constants.
	 */
	private static final Logger LOGGER = Logger.getLogger(ActivityDocumentationController.class);
	private static final long serialVersionUID = -4266031141074398764L;

	/**
	 * Members.
	 */
	private Activity currentActivityToDocument;
	private ActivityAction activityActionToCollect = new ActivityAction();
	@Autowired
	private transient ActivityManager activityManager;
	private ActivityParameter activityParameterToCollect = new ActivityParameter();
	private boolean activityDocumented = false, errorActionsMissing = false, errorParemeterNotUnique = false;
	private List<ActivityAction> activityActionsToAdd;
	private List<ActivityParameter> activityParametersToAdd;

	/**
	 * Default builder;
	 */
	public ActivityDocumentationController() {
		super();
		activityActionsToAdd = new ArrayList<ActivityAction>();
		activityParametersToAdd = new ArrayList<ActivityParameter>();
	}

	/**
	 * Initializes the activity action to collect object.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param e the event launched on this action.
	 * @since July, 2011.
	 */
	public String initActivityActionToCollect() {
		LOGGER.debug("initActivityActionToCollect().");
		activityActionToCollect = new ActivityAction();
		return null;
	}

	/**
	 * Initializes the activity parameter to collect object.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param e the event launched on this action.
	 * @since July, 2011.
	 */
	public String initActivityParameterToCollect() {
		LOGGER.debug("initActivityParameterToCollect().");
		activityParameterToCollect = new ActivityParameter();
		return null;
	}

	/**
	 * Initializes the error fields.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @since July, 2011.
	 */
	private void initErrorFields() {
		errorActionsMissing = false;
		errorParemeterNotUnique = false;
	}

	/**
	 * Event listener on activity documentation.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @param event the event launched on this action.
	 * @since July, 2011.
	 */
	public void onActivityDocumentation(ActionEvent event) {
		LOGGER.debug("onActivityDocumentation().");
		initErrorFields();
		if (activityActionsToAdd.isEmpty()) {
			errorActionsMissing = true;
			return;
		}
		if (!areParametersUniques()) {
			errorParemeterNotUnique = true;
			return;
		}
		try {
			activityManager.createActivityDocumentation(currentActivityToDocument, activityActionsToAdd, activityParametersToAdd);
			activityActionToCollect = new ActivityAction();
			activityActionsToAdd = new ArrayList<ActivityAction>();
			activityDocumented = true;
			activityParametersToAdd = new ArrayList<ActivityParameter>();
			activityParameterToCollect = new ActivityParameter();
		} catch (ActivityDocumentationCreationManagerException e) {
			LOGGER.error("Error during the activity documentation creation.", e);
		}
	}

	/**
	 * Checks if every parameter is unique. It's based on their names.
	 * 
	 * @author Clement HELIOU (clement.heliou@che-software.com).
	 * @return true if every parameter is unique, else false.
	 * @since July, 2011.
	 */
	private boolean areParametersUniques() {
		LOGGER.debug("areParametersUniques().");
		for (ActivityParameter parameter : activityParametersToAdd) {
			for (ActivityParameter otherParameter : activityParametersToAdd) {
				if (parameter.getName().equalsIgnoreCase(otherParameter.getName()) && !parameter.toString().equalsIgnoreCase(otherParameter.toString())) {
					return false;
				}
			}
		}
		return true;
	}

	/**
	 * Getter for the private field value currentActivityToDocument.
	 * 
	 * @return the currentActivityToDocument field value.
	 */
	public Activity getCurrentActivityToDocument() {
		return currentActivityToDocument;
	}

	/**
	 * Setting a value to the currentActivityToDocument field.
	 * 
	 * @param currentActivityToDocument the value to set.
	 */
	public void setCurrentActivityToDocument(Activity currentActivityToDocument) {
		LOGGER.debug("A new value is setted in the currentActivityToDocument field. Then, the related boolean is setted to false.");
		activityDocumented = false;
		this.currentActivityToDocument = currentActivityToDocument;
	}

	/**
	 * Getter for the private field value activityActionToCollect.
	 * 
	 * @return the activityActionToCollect field value.
	 */
	public ActivityAction getActivityActionToCollect() {
		return activityActionToCollect;
	}

	/**
	 * Setting a value to the activityActionToCollect field.
	 * 
	 * @param activityActionToCollect the value to set.
	 */
	public void setActivityActionToCollect(ActivityAction activityActionToCollect) {
		this.activityActionToCollect = activityActionToCollect;
	}

	/**
	 * Getter for the private field value activityParameterToCollect.
	 * 
	 * @return the activityParameterToCollect field value.
	 */
	public ActivityParameter getActivityParameterToCollect() {
		return activityParameterToCollect;
	}

	/**
	 * Setting a value to the activityParameterToCollect field.
	 * 
	 * @param activityParameterToCollect the value to set.
	 */
	public void setActivityParameterToCollect(ActivityParameter activityParameterToCollect) {
		this.activityParameterToCollect = activityParameterToCollect;
	}

	/**
	 * Getter for the private field value activityActionsToAdd.
	 * 
	 * @return the activityActionsToAdd field value.
	 */
	public List<ActivityAction> getActivityActionsToAdd() {
		return activityActionsToAdd;
	}

	/**
	 * Setting a value to the activityActionsToAdd field.
	 * 
	 * @param activityActionsToAdd the value to set.
	 */
	public void setActivityActionsToAdd(List<ActivityAction> activityActionsToAdd) {
		this.activityActionsToAdd = activityActionsToAdd;
	}

	/**
	 * Getter for the private field value activityParametersToAdd.
	 * 
	 * @return the activityParametersToAdd field value.
	 */
	public List<ActivityParameter> getActivityParametersToAdd() {
		return activityParametersToAdd;
	}

	/**
	 * Setting a value to the activityParametersToAdd field.
	 * 
	 * @param activityParametersToAdd the value to set.
	 */
	public void setActivityParametersToAdd(List<ActivityParameter> activityParametersToAdd) {
		this.activityParametersToAdd = activityParametersToAdd;
	}

	/**
	 * Getter for the private field value errorParemeterNotUnique.
	 * 
	 * @return the errorParemeterNotUnique field value.
	 */
	public boolean isErrorParemeterNotUnique() {
		return errorParemeterNotUnique;
	}

	/**
	 * Setting a value to the errorParemeterNotUnique field.
	 * 
	 * @param errorParemeterNotUnique the value to set.
	 */
	public void setErrorParemeterNotUnique(boolean errorParemeterNotUnique) {
		this.errorParemeterNotUnique = errorParemeterNotUnique;
	}

	/**
	 * Getter for the private field value errorActionsMissing.
	 * 
	 * @return the errorActionsMissing field value.
	 */
	public boolean isErrorActionsMissing() {
		return errorActionsMissing;
	}

	/**
	 * Setting a value to the errorActionsMissing field.
	 * 
	 * @param errorActionsMissing the value to set.
	 */
	public void setErrorActionsMissing(boolean errorActionsMissing) {
		this.errorActionsMissing = errorActionsMissing;
	}

	/**
	 * Getter for the private field value activityDocumented.
	 * 
	 * @return the activityDocumented field value.
	 */
	public boolean isActivityDocumented() {
		return activityDocumented;
	}

	/**
	 * Setting a value to the activityDocumented field.
	 * 
	 * @param activityDocumented the value to set.
	 */
	public void setActivityDocumented(boolean activityDocumented) {
		this.activityDocumented = activityDocumented;
	}

	/**
	 * Getter for the private field value activityManager.
	 * 
	 * @return the activityManager field value.
	 */
	public ActivityManager getActivityManager() {
		return activityManager;
	}

	/**
	 * Setting a value to the activityManager field.
	 * 
	 * @param activityManager the value to set.
	 */
	public void setActivityManager(ActivityManager activityManager) {
		this.activityManager = activityManager;
	}
}